package net.maku.association.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.common.utils.Result;
import net.maku.association.convert.InterestAssociationApplyConvert;
import net.maku.association.entity.InterestAssociationApplyEntity;
import net.maku.association.service.InterestAssociationApplyService;
import net.maku.association.query.InterestAssociationApplyQuery;
import net.maku.association.vo.InterestAssociationApplyVO;
import net.maku.framework.security.user.SecurityUser;
import net.maku.framework.security.user.UserDetail;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;

import java.util.Date;
import java.util.List;

/**
* 申请加入协会
*
* @author 阿沐 babamu@126.com
* @since 1.0.0 2025-04-26
*/
@RestController
@RequestMapping("association/apply")
@Tag(name="申请加入协会")
@AllArgsConstructor
public class InterestAssociationApplyController {
    private final InterestAssociationApplyService interestAssociationApplyService;

    @GetMapping("page")
    @Operation(summary = "分页")
    public Result<PageResult<InterestAssociationApplyVO>> page(@ParameterObject @Valid InterestAssociationApplyQuery query){
        PageResult<InterestAssociationApplyVO> page = interestAssociationApplyService.page(query);

        return Result.ok(page);
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
    public Result<InterestAssociationApplyVO> get(@PathVariable("id") Long id){
        InterestAssociationApplyEntity entity = interestAssociationApplyService.getById(id);

        return Result.ok(InterestAssociationApplyConvert.INSTANCE.convert(entity));
    }

    @PostMapping
    @Operation(summary = "申请")
    public Result<String> save(@RequestBody InterestAssociationApplyVO vo){
        Long userId = SecurityUser.getUserId();
        UserDetail user = SecurityUser.getUser();
        vo.setUserId(userId);
        vo.setRealName(user.getRealName());
        vo.setApplyTime(new Date());
        vo.setCreateTime(new Date());
        interestAssociationApplyService.save(vo);

        return Result.ok();
    }

}